<?php
/**
 *个人中心类
 */
namespace App\Controller\Member;
use Common\Controller\App\CommonController;
use Common\Model\LogisticsExpressModel;
use Common\Model\MemberModel;
use Common\Model\MicroShopCouponsConfigModel;
use Common\Model\MicroShopCouponsModel;
use Common\Model\OrderModel;
use Common\Model\QuanfanCountModel;
use Common\Model\QuanfanRecordModel;
use Common\Model\TarentoCashFlowModel;
use Common\Model\TarentoModel;
use Common\Model\CaptchaModel;

class IndexController extends CommonController{

    /**
     * 全返红包领取
     */
    public function quanfan_receive(){
        $member_id = session('member_id');
        $id = I('post.id');
        $model = new QuanfanRecordModel();
        $m_model = new TarentoModel();
        $result = $model->where("id=$id and member_id=member_id")->save(['status'=>1]);
        if($result){
            $info = $model->where("id=$id and member_id=member_id")->find();
            $money = $info['returned_money'];   //tarento_cash_flow
            $account = floatval($info['account'])+floatval($money);
            $m_model->where("member_id = $member_id")->save(['account'=>$account]);
            $t_model = new TarentoCashFlowModel();
            $data['price'] = $money;
            $data['member_id'] = $member_id;
            $data['description'] = '全返活动服务号推广奖励';
            $data['create_time'] = date('Y-m-d H:i:s',time());
            $t_model->add($data);
            $this->ajaxReturn(['code'=>200,'price'=>$money]);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'红包领取失败，请稍后再试！']);
        }
    }

    /**
     * 判断是否是新用户
     */
    public function member_info(){
        $member_id = session('member_id');
        $memberModel = new MemberModel();
        $info = $memberModel->where("userid=$member_id")->field("mobile")->find();
        $this->ajaxReturn(['code'=>200,'mobile'=>$info['mobile']]);
    }

    /**
     * 领取红包
     */
    public function receive_red(){
        $member_id = session('member_id');
        $mobile = I('post.mobile');
        $code = I('post.code');
        if(!preg_match("/^1[34578]\d{9}$/", $mobile)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请输入正确的手机号码']);
        }
        if(empty($code)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请输入手机验证码']);
        }
        $model = new CaptchaModel();
        $result = $model->where("unique_str=$mobile and captcha=$code")->order('id desc')->find();
        $model->where("unique_str=$mobile")->delete();
        if(empty($result)){
            $this->ajaxReturn(['code'=>300,'msg'=>'手机验证码输入错误']);
        }
        if(time()>strtotime($result['invalid_time'])){
            $this->ajaxReturn(['code'=>300,'msg'=>'手机验证码已过期，请输入发送']);
        }
        $model = new MemberModel();
        $info = $model->where("mobile='$mobile' and userid=$member_id")->find();
        if(empty($info)){
            $model->where("userid=$member_id")->save(['mobile'=>$mobile]);
        }
        $cnpsMoel = new MicroShopCouponsConfigModel();
        $cnpmodel1 = new MicroShopCouponsModel();
        $info = $cnpsMoel->where("id=11")->field("name,config_json")->find();
        $infos = $cnpmodel1->where("member_id=$member_id and package_id=1")->find();
        if($infos){
            $this->ajaxReturn(['code'=>300,'msg'=>'代金券不能重复领取']);
        }
        $config_info = json_decode($info['config_json']);
        if($config_info){
            $num = $config_info['cpns_num'];   //overtime
            $time = 3600*24*$config_info['overtime']+time();
            foreach($num as $v){
                $data['end_time'] = date('Y-m-d H:i:s',$time);
                $data['start_time'] = date('Y-m-d H:i:s',time());
                $data['package_id'] = 11;
                $data['title'] = $info['title'];
                $data['cpns_then'] = $config_info['cpns_then'];
                $data['member_id'] = $member_id;
                $data['cpns_key'] = $this->createCouponsCode();
                $result = $cnpmodel1->add($data);
            }
        }
        $this->ajaxReturn(['code'=>200,'msg'=>'代金券领取成功']);
    }

    /**
     * 创建优惠码
     * 不会重复
     * @param int $length
     * @param string $codePre
     * @return string
     */
    public function createCouponsCode($length = 10, $codePre = ""){
        $codeArr = array();

        for ($i = 0; $i < ($length- strlen($codePre)); $i ++) {
            $type = mt_rand(1, 2);

            if($i == 0 || $type == 1){//字母，保证第一个是字母
                $ascii = "";

                while (true) {
                    $ascii = mt_rand(65, 90);
                    if ($ascii != 79 && $ascii != 73) {
                        // 排除O字母和I
                        break;
                    }
                }
                $codeArr[] = chr($ascii);
            }else{//数字
                $num = mt_rand(2, 9);
                $codeArr[] = $num;
            }
        }

        $code = implode('', $codeArr);

        strlen($codePre) > 0 && $code = $codePre.$code;
        $cnpmodel1 = new MicroShopCouponsModel();
        $cpns_key_info = $cnpmodel1->where("cpns_key='$code'")->find();
        if($cpns_key_info){
            $this->createCouponsCode();
        }
        return $code;
    }

    /**
     * 查看物流接口
     */
    public function logistics(){
        $order_id = I('post.order_id');
        $model = new OrderModel();
        $info = $model->where("order_id='$order_id'")->field("shipping_no,shipping_express_id,shipping_express_name")->find();
        $id = $info['shipping_express_id'];
        $shipping_no = $info['shipping_no'];
        //logistics_express
        $model = new LogisticsExpressModel();
        $logic_info = $model->where("id=$id")->field("sign")->find();
        $com = $logic_info['sign'];
        $info = logistics($shipping_no,$com);

        $this->ajaxReturn(['code'=>200,'info'=>$info]);
    }

    /**
     * 个人中心首页
     */
    public function index(){
        $member_id = session('member_id');
        $model = new MemberModel();
        $info = $model->index($member_id);
        $this->ajaxReturn(['code'=>200,'info'=>$info]);
    }

    /**
     * 我的订单
     */
    public function order_list(){
        $page = I('post.page')?I('post.page'):1;
        $status = I('post.status')?I('post.status'):5;
        $limit = I('post.limit')?I('post.limit'):20;
        $member_id = session('member_id');
        $num = ($page-1)*$limit;
        $model = new OrderModel();
        $list = $model->order_list($member_id,$num,$limit,$status);
        $this->ajaxReturn(['code'=>200,'list'=>$list,'status'=>$status]);
    }
    /**
     * 订单详情
     */
    public function order_details(){
        $member_id = session('member_id');
        $order_id  = I('post.order_id');
        if(empty($order_id)){
            $this->ajaxReturn(['code'=>300,'msg'=>'订单编号错误']);
        }
        $model = new OrderModel();
        $order_info = $model->order_details($member_id,$order_id);
        $this->ajaxReturn(['code'=>200,'order_info'=>$order_info]);
    }
    /**
     * 取消订单
     */
    public function order_clear(){
        $member_id = session('member_id');
        $order_id  = I('post.order_id');
        if(empty($order_id)){
            $this->ajaxReturn(['code'=>300,'msg'=>'订单编号错误']);
        }
        $model = new OrderModel();
        $result = $model->where("member_id=$member_id and order_id=$order_id")->save(['status'=>0]);
        if($result){
            $this->ajaxReturn(['code'=>200,'msg'=>'订单取消成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'订单取消失败，请稍后再试']);
        }
    }

    /**
     * 订单支付
     */
    public function order_pay(){
        $order_id = I('post.order_id');
        if(empty($order_id)){
            $this->ajaxReturn(['code'=>300,'msg'=>'订单编号错误']);
        }
        $member_id = session('member_id');
        $payTypeId  = I('post.payTypeId')?I('post.payTypeId'):2; //支付类型id   1支付宝 2微信
        $order_total_money = 0; //订单总金额
        $model = new OrderModel();
        $order_info = $model->where("order_id=$order_id and member_id=$member_id")->field("id,order_id,create_time,status,cost,cash_price,total_ms_taxation,ms_freight_risk,freight,cpns_price")->find();
        $order_total_money += floatval($order_info['cost'])-floatval($order_info['cpns_price'])+floatval($order_info['freight'])+floatval($order_info['ms_freight_risk'])+floatval($order_info['total_ms_taxation'])-floatval($order_info['cash_price']);

        if($payTypeId==1){
            require(CORE_PATH.'Alipay/alipayapi.php');
        }
    }

    /**
     * 确认收货
     */
    public function confirm_receipt(){
        $order_id = I('post.order_id');
        $member_id = session('member_id');
        $model = new OrderModel();
        $res = $model->confirm_receipt($member_id,$order_id);
        $this->ajaxReturn($res);
    }

    /**
     * 我的账单
     */
    public function my_bill(){
        $member_id = session('member_id');
        $model = new QuanfanCountModel();
        $info = $model->getOne($member_id);
        $model = new QuanfanRecordModel();
        $list = $model->getList($member_id);
        $this->ajaxReturn(['code'=>200,'info'=>$info,'list'=>$list]);
    }
    /**
     * 余额明细
     */
    public function cash_list(){
        $member_id = session('member_id');
        $page = I('post.page')?I('post.page'):1;
        $limit = I('post.limit')?I('post.limit'):20;
        $num = ($page-1)*$limit;
        $model = new TarentoCashFlowModel();
        $list = $model->getlist($member_id,$num,$limit);
        $this->ajaxReturn(['code'=>200,'list'=>$list]);
    }
    /**
     * 粉丝列表(吃货)
     */
    public function fans_chowhound_list(){
        $member_id = session('member_id');
        $page = I('post.page')?I('post.page'):1;
        $limit = I('post.limit')?I('post.limit'):20;
        $num = ($page-1)*$limit;
        $model = new TarentoModel();
        $list = $model->getChildShopsIdByLevel($member_id,$num,$limit);
        $this->ajaxReturn(['code'=>200,'list'=>$list]);
    }
}